// request.js
export default {
    config: {
        // baseURL: "http://192.168.1.200:20001",
        // baseURL:'http://118.89.122.156:20001',
        // baseURL:'http://44c434d9.r19.cpolar.top',
        baseURL:'https://huijuniot.net.cn/prod-api',
        getToken() {
            let token = uni.getStorageSync("token");
            if (!token) {
                wx.navigateTo({
                    url: "/pages/login/index"
                });
                return null; // 返回 null 或者抛出异常
            }
            return token;
        },      
        // 响应拦截器
        handleResponse(data) {
            const userInfo = uni.getStorageSync("userInfo");
            return data;
        },
    },
    // request 请求
    request(options = {}) {
        return new Promise((resolve, reject) => {
            // options.url = this.config.baseURL +'/prod-api'+ options.url;
            options.url = this.config.baseURL+ options.url;
            console.log(options.url,'options.url');
            options.method = options.method || "POST";
            options.header = {
                Authorization: this.config.getToken(),
            };
            console.log(options, "request options");
            wx.request({
                ...options,
                header: options.header, // 添加请求头信息
                success: (res) => {
                    resolve(this.config.handleResponse(res));
                },
                fail: (err) => {
                    reject(err);
                },
            });
        });
    },
};